module UsersHelper
    include ApplicationHelper
    include IndexHelper
    
    def loadUsers start, offset, cond = nil
        @render_data = ""
        if cond.nil?
          @employees = (getFirstEmployees.find(:limit => [start, (start.to_i + offset.to_i)], :order => "Name") )
        else
          @employees = getFirstEmployees.find(:limit => [start, (start.to_i + offset.to_i)], :order => "Name", :conditions => cond)
        end
        
        @employees.each do |employee|
            @render_data += "<tr>"
            @render_data += getContextSpecificIcon "user"
            if !employee.EmployeeNumber.empty?
              strUserDetailsUrl = "http://" + request.env["HTTP_HOST"] + "/listusersql.asp?userid=" + employee.EmployeeType
              strMapUrl = getMapUrl employee.BusinessUnit
              strOfficeLayoutUrl = getOfficeLayoutUrl employee.BusinessUnit, employee.Name
              strConferenceRoomUrl = getConferenceRoomUrl employee.BusinessUnit
                                    
              @render_data += "<td align=\"left\" class=\"namestd notop\">"
              @render_data += "<a class=\"empdetails\" onclick=\"javascript:window.gbl_var.popup.ShowUserDetails('#{employee.EmployeeNumber}','#{employee.BusinessUnit}','#{strMapUrl}', this, event,'#{strOfficeLayoutUrl}','#{strConferenceRoomUrl}'); return false;\" href='#'>#{employee.Name}</a>"
              if !employee.SkypeName.nil? and !employee.SkypeName.empty?
                  @render_data += "<a href='skype:#{employee.SkypeName}?chat'><img src='/images/skype.png' border='0' /></a>"
              end
              if !employee.EMail.nil? and !employee.EMail.empty?
                  @render_data += "<a href=\"mailto:#{employee.EMail}\" ><img src=\"images/ico_email.png\" border=\"0\"></a>"
              end
              @render_data += "</td>"
            else
              @render_data += "<td align=\"left\" class=\"namestd notop\"><a href='javascript:void(0);'>#{employee.Name}</a></td>"
            end
          
          
            if !employee.BusinessUnit.nil? and !employee.BusinessUnit.empty?
                @render_data += "<td align=\"left\" class=\"valuegreen notop filterBusinessUnit\" title=\"Click here to Filter\">"
                    @render_data += "&nbsp;<a href='javascript:void(0);' style=\"display:inline-block;border-width:0px;\">#{employee.BusinessUnit}</a>"
                @render_data += "</td>"
                @render_data += "<td align=\"left\" class=\"valuegreen notop\" >&nbsp;</td>"
                @render_data += "<td align=\"left\" class=\"valuegreen notop filterDepartment\" title=\"Click here to Filter\"><a href='javascript:void(0);'>#{employee.Department}</a></td>"
                @render_data += "<td align=\"left\" class=\"valuegreen notop\" >&nbsp;</td>"
                                    
                @render_data += "<td align=\"left\" class=\"valuegreen notop filterJobTitle\" title=\"Click here to Filter\"><a href='javascript:void(0);'>#{employee.JobTitle}</a></td>"
                @render_data += "<td align=\"left\" class=\"valuegreen notop\" >&nbsp;</td>"
                                
                @render_data += "<td align=\"left\" class=\"valuegreen notop\" notop\"\">#{employee.Office}</td>"
             else
                @render_data += "<td class=\"valuegreen notop\">&nbsp;</td>"
                @render_data += "<td align=\"left\" class=\"valuegreen notop\">&nbsp;</td>"
                @render_data += "<td class=\"valuegreen notop\">&nbsp;</td>"
                @render_data += "<td align=\"left\" class=\"valuegreen notop\" >&nbsp;</td>"
                @render_data += "<td class=\"valuegreen notop\">&nbsp;</td>"
                @render_data += "<td align=\"left\" class=\"valuegreen notop\" >&nbsp;</td>"
                @render_data += "<td align=\"left\" class=\"valuegreen notop\" >&nbsp;</td>"
              end
           @render_data += "</tr>"
        end
        @render_data
    end
    
    def getSQLCondition params
        exclude_array = ["controller", "action", "start", "offset"];
        condition_sql = ""
        params.keys.each do |key|
            if !(exclude_array.include? key)
              condition_sql += key.inspect.gsub( /\"/m, "" ) + "='" + params[key] + "' AND "
            end
        end
        return condition_sql[0..-5]
    end
end